package com.qc.sys.web;

import com.qc.common.utils.StringUtils;
import com.qc.common.web.BaseController;
import com.qc.sys.entity.Menu;
import com.qc.sys.service.SystemService;
import com.qc.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 菜单Controller
 */
@Controller
@RequestMapping(value = "sys/menu")
public class MenuController extends BaseController {

	@Autowired
	private SystemService systemService;

	@ModelAttribute("menu")
	public Menu get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return systemService.getMenu(id);
		}else{
			return new Menu();
		}
	}

	@RequiresPermissions("sys:menu:view")
	@RequestMapping(value = {"list", ""})
	public String list(Model model) {
		List<Menu> sourcelist = systemService.findMenuList();
        model.addAttribute("list", sourcelist);
		setTitle(model, "菜单列表");
		return "sys.menu.menuList";
	}

	@RequiresPermissions("sys:menu:view")
	@RequestMapping(value = "form")
	public String form(Menu menu, String parentId,Model model) {
		parentId = menu.getParentId()!=null?menu.getParentId():parentId;
		if(parentId!=null){
			Menu parentMenu = systemService.getParnetMenu(parentId);
			if(parentMenu.getName()!=null)
				menu.setParentName(parentMenu.getName());
			menu.setParentId(parentMenu.getNodeId());
		}
		model.addAttribute("menu", menu);
		if (StringUtils.isNotBlank(menu.getId())) {
			setTitle(model, "菜单编辑");
		} else {
			setTitle(model, "菜单新增");
			String menuSort = systemService.getMaxSort();
			model.addAttribute("maxSort",menuSort);
		}
		return "sys.menu.menuForm";
	}

	@RequiresPermissions("sys:menu:edit") //管理员角色才能删除菜单
	@RequestMapping(value = "save")
	public String save(Menu menu, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) {
		if(!UserUtils.getCurrentUser().isAdmin()){
			addMessage(redirectAttributes, "越权操作，只有超级管理员才能添加或修改数据！");
			return "redirect:/sys/role/?repage";
		}
		if (!beanValidator(model, menu)){
			return form(menu,menu.getParentId(), model);
		}

		menu.setNodeId(menu.getParentId()+String.valueOf(menu.getNodeValue()));
		systemService.saveMenu(menu);
		addMessage(redirectAttributes, "保存菜单 < " + menu.getName() + " > 成功");
		redirectAttributes.addFlashAttribute("redirect", request.getContextPath()+"/sys/menu");
		return "redirect:/sys/menu";
	}

	@RequiresPermissions("sys:menu:edit") //管理员角色才能删除菜单
	@RequestMapping(value = "delete")
	public String delete(Menu menu, RedirectAttributes redirectAttributes, HttpServletRequest request,HttpServletResponse response) {
		systemService.deleteMenu(menu.getId());
		addMessage(redirectAttributes, "删除菜单成功");
		redirectAttributes.addFlashAttribute("redirect", request.getContextPath()+"/sys/menu");
		return "redirect:/sys/menu";
	}

	/**
	 * 批量修改菜单排序
	 *//*
	@RequiresPermissions("sys:menu:edit")
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes, HttpServletRequest request) {
    	for (int i = 0; i < ids.length; i++) {
    		Menu menu = new Menu(ids[i]);
//    		menu.setSort(sorts[i]);
//    		systemService.updateMenuSort(menu);
    	}
    	addMessage(redirectAttributes, "保存菜单排序成功!");
		redirectAttributes.addFlashAttribute("redirect", request.getContextPath()+"/sys/menu");
		return "redirect:/sys/menu";
	}*/

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(String defaultSelectedId, String defaultCheckedId) {
		return UserUtils.getMenuTreeList(defaultSelectedId, defaultCheckedId);
	}
}
